package com.mego.tzadd.database.manager;


import android.database.sqlite.SQLiteException;

import com.mego.tzadd.activitys.AppContext;
import com.mego.tzadd.database.dbmodel.Sendmsg;
import com.mego.tzadd.database.greendao.SendmsgDao;

import java.util.ArrayList;


public class SendsmsCenter {

    private static SendsmsCenter instance = new SendsmsCenter();
    public ArrayList<Sendmsg> mSendsmsList = new ArrayList<Sendmsg>();

    public static SendsmsCenter getInstance() {
        return instance;
    }

    //查找历史记录
    public Sendmsg getSendmsg(long date, String content) {
        Sendmsg sendmsg = null;
        try {
            SendmsgDao systemAllInfoDao = AppContext.getInstances().getDaoSession().getSendmsgDao();
            ArrayList<Sendmsg> smsList = new ArrayList<Sendmsg>();
            smsList = (ArrayList) systemAllInfoDao.queryBuilder()
                    .where(SendmsgDao.Properties.Date.eq(date))
                    .where(SendmsgDao.Properties.Content.eq(content))
                    .build().list();
            if (smsList.size() > 0) {
                sendmsg = smsList.get(0);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return sendmsg;
    }

    public Long saveSendmsg(Sendmsg smslog) {
        if (smslog == null) {
            return 0L;
        }

        long id = 0;
        try {
            SendmsgDao smslogDao = AppContext.getInstances().getDaoSession().getSendmsgDao();
            id = smslogDao.insertOrReplace(smslog);
            smslog.setId(id);
        } catch (SQLiteException e) {
            e.printStackTrace();
        }

        return id;
    }

}
